package com.xuerhua.yxt.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.RelationType;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.PrincipalMailbox;
import com.xuerhua.yxt.page.OrderBy;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.MailboxReqVO;
import com.xuerhua.yxt.service.IPrincipalMailboxService;
import com.xuerhua.yxt.view.PrincipalMailboxView;
import com.xuerhua.yxt.vo.PrincipalMailboxQueryVO;

@Controller
public class MailboxPrincipalController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(MailboxPrincipalController.class);
	
	@Autowired
	private IPrincipalMailboxService principalMailboxService;

	@RequestMapping(Const.TEACHER_ROLE + MenuConst.MAILBOX_PRINCIPAL)
	public String mailboxPrincipal(Model model){
		model.addAttribute("relationType", RelationType.MAILBOX.getCode());
		return "teacher/principalMailbox";
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.MAILBOX_PRINCIPAL)
	public String studentMailboxPrincipal(Model model){
		model.addAttribute("relationType", RelationType.MAILBOX.getCode());
		return "student/principalMailbox";
	}
	
	@RequestMapping(MenuConst.MAILBOX_PRINCIPAL + "/page/query")
	@ResponseBody Object principalMailboxQuery(PrincipalMailboxQueryVO vo) {
		try {
			Integer schoolId = getAppUser().getRoleType()==1 ? getAppUser().getTeacher().getSchoolId() : getAppUser().getParent().getSchoolId();
			Page<PrincipalMailboxView> page = new Page<PrincipalMailboxView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			vo.setSchoolId(schoolId);
			sorts.add(new OrderBy("CREATE_TIME", "desc"));
			sorts.add(new OrderBy("REPLY_TIME", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<PrincipalMailboxView> list = principalMailboxService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---principalMailboxQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.MAILBOX_PRINCIPAL + "/create")
	@ResponseBody Object studentMailboxPrincipalCreate(MailboxReqVO record) {
		try {
			AppUser appUser = getAppUser();
			record.setTermId(getCurrentTerm().getId());
			record.setParentId(appUser.getParent().getId());
			record.setSchoolId(appUser.getParent().getSchoolId());
			record.setSchoolName(appUser.getParent().getSchoolName());
			record.setStudentId(appUser.getParent().getStudentId());
			if(record.getIds() != null) {
				record.setIsAttach(1);
			}
			//TODO 不用添加老师吗？
			principalMailboxService.insert(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("----studentMailboxPrincipalCreate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.MAILBOX_PRINCIPAL + "/reply")
	@ResponseBody Object studentMailboxPrincipalReply(PrincipalMailbox record) {
		try {
			AppUser appUser = getAppUser();
			record.setTeacherId(appUser.getTeacher().getId());
			record.setTeacherName(appUser.getTeacher().getName());
			record.setReplyTime(new Date());
			record.setIsReply(1);
			record.setSchoolId(appUser.getTeacher().getSchoolId());
			principalMailboxService.updateByPrimaryKeySelective(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---studentMailboxPrincipalReply error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
